package com.example.cloud.service.basic.config.impl;

import com.example.cloud.dao.basic.config.CreditItemsRepository;
import com.example.cloud.dao.mongdb.basic.config.MongoCreditItemsRepository;
import com.example.cloud.model.statistics.CreditItems;
import com.example.cloud.model.system.User;
import com.example.cloud.service.basic.config.CreditItemsService;
import com.example.cloud.utils.BusinessUtil;
import com.example.cloud.utils.RequestUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by sl on 2019/04/24.
 */
@Service
public class CreditItemsServiceImpl implements CreditItemsService {

    private static final Logger logger = Logger.getLogger(CreditItemsServiceImpl.class);

    @PersistenceContext
    EntityManager em;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private CreditItemsRepository creditItemsRepository;

    @Autowired
    private MongoCreditItemsRepository mongoCreditItemsRepository;

    @Override
    public List<CreditItems> findAllFromCache() {
        User user = RequestUtil.getLoginUser();
        return mongoCreditItemsRepository.findByAccountIdAndOrganIdAndDeleteFlag(user.getAccountId(), user.getOrganId(), 0);
    }

    @Override
    @Transactional
    public void doSave(CreditItems record) {
        // 新增
        if(record.getId() == null){
            User loginUser = RequestUtil.getLoginUser();
            record.setAccountId(loginUser.getAccountId());
            record.setOrganId(loginUser.getOrganId());
            record.setOrganName(loginUser.getOrganName());
        }
        BusinessUtil.appendRecord(record);
        // 物理保存
        creditItemsRepository.save(record);
        // 同步缓存
        mongoCreditItemsRepository.save(record);
    }

    @Override
    @Transactional
    public void doDelete(List<Long> ids) {
        // 逻辑删除
        StringBuilder sql = new StringBuilder("update credit_items set delete_flag = 1 where id in(:ids)");
        Query query = em.createNativeQuery(sql.toString());
        query.setParameter("ids", ids);
        query.executeUpdate();
        // 删除缓存
        mongoCreditItemsRepository.deleteByIdIn(ids);
    }

    @Override
    public void loadCache() {
        List<CreditItems> list = creditItemsRepository.findAll();
        if(!list.isEmpty()){
        	mongoCreditItemsRepository.save(list);
            logger.info("*******************信用制度已全部同步缓存****;i***************");
        }
    }
    
    @Override
    public List<CreditItems> findCreditItems(int creditType) {
      User user = RequestUtil.getLoginUser();
      return mongoCreditItemsRepository.findByAccountIdAndOrganIdAndCreditTypeAndDeleteFlag(user.getAccountId(), user.getOrganId(), creditType, 0);

    }

}
